﻿#region

using System;
using System.Web.Mvc;
using CuongHanh.Configuration;
using CuongHanh.Extensions;
using CuongHanh.Models;
using CuongHanh.Models.NotMapDB;
using CuongHanh.Service;

#endregion

namespace CuongHanh.Controllers.FO
{
    public class CustomerController : BaseController
    {
        [HttpGet]
        public ActionResult Register()
        {
            if(Session.GetCustomerId() > 0)
                return RedirectToAction("Index",
                        "Gem");
            return View();
        }

        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Register(CustomerModel model)
        {
            if(ModelState.IsValid)
            {
                Customer customer = new Customer(model);
                bool result = CustomerServce.Insert(customer);
                if(result)
                {
                    Session.WriteCustomerInfo(customer.Id,
                            customer.FullName);
                    return RedirectToAction("Index",
                            "Gem");
                }
                   
               
            }
            ViewBag.ErrorMessage = ErrorList.UsernameOrEmailExits(GetLanguage());
            return View("Register",
                    model);
        }

        [HttpGet]
        public ActionResult Login()
        {
            if(Session.GetCustomerId() > 0)
                return RedirectToAction("Index",
                        "Gem");
            return View();
        }

        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Login(CustomerLogin model,
                string returnUrl)
        {
            if(string.IsNullOrWhiteSpace(model.Username)
               || string.IsNullOrWhiteSpace(model.Password))
            {
                ViewBag.LoginMessage = ErrorList.UserPassIsNotEmpty(GetLanguage());
                return View(model);
            }

            Customer customer = CustomerServce.Login(model);
            if(customer != null)
            {
               
                Session.WriteCustomerInfo(customer.Id,customer.FullName);
                return RedirectToAction(returnUrl);
            }
            ViewBag.LoginMessage = ErrorList.UserPassInValid(GetLanguage());
            return View(model);
        }

        [HttpGet]
        public ActionResult ForgotPassword()
        {
            if(Session.GetCustomerId() > 0)
                return RedirectToAction("Index",
                        "Gem");
            return View();
        }

        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult ForgotPassword(ResetPassWord model)
        {
            if(ModelState.IsValid)
            {
                ViewBag.Message = CustomerServce.ForgotPassword(model)
                        ? ErrorList.SendLinkResetPass(GetLanguage())
                        : ErrorList.EmailInvalid(GetLanguage());
                return View(model);
            }
            return View(model);
        }

        [HttpGet]
        public ActionResult ChangePasswordByForgot(string resetKey)
        {
            if(Session.GetCustomerId() > 0)
                return RedirectToAction("Index",
                        "Gem");
            return View(CustomerServce.GetCustomerFromResetKey(resetKey));
        }

        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult ChangePasswordByForgot(ChangePasswordByForgot model)
        {
            if(!ModelState.IsValid)
                return View(model);
            ViewBag.Message = CustomerServce.ChangePasswordByForgot(model)
                    ? ErrorList.ResetPasswordSuccess(GetLanguage())
                    : ErrorList.ResetPasswordFailed(GetLanguage());
            return View(model);
        }

        public ActionResult LogOff()
        {
            Session.Clear();

            return RedirectToAction("Index",
                    "Gem");
        }

        [HttpGet]
        public ActionResult ChangePassword()
        {
            if (Session.GetCustomerId() == 0)
                return RedirectToAction("Index",
                        "Gem");
            ViewBag.GetPage = "get";
            return View();
        }

        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult ChangePassword(ChangePassword model)
        {
            if (!ModelState.IsValid)
                return View(model);
            ViewBag.Message = CustomerServce.ChangePassword(model,GetCustomerId())
                    ? ErrorList.ResetPasswordSuccess(GetLanguage())
                    : ErrorList.ResetPasswordFailed(GetLanguage());
            return View("ChangePassword",model);
        }
    }
}
